<?php


namespace app\partneradmin\controller;


use app\partneradmin\model\User;
use app\common\controller\partneradmin\Base;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Withdrawalorder extends Base
{
    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\partneradmin\model\WithdrawalOrder();
    }


    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $page = $this->request->post('page', 1);
            $size = $this->request->post('page_size', 10);
            $sort = $this->request->post('sort', 'id');
            $order = $this->request->post('order', 'desc');
            $cash_out_num = $this->request->post('cash_out_num', '');
            $user_id = $this->request->post('user_id', '');
            $status = $this->request->post('status', '');
            $customerServicePersonnelId = $this->request->post('customer_service_personnel_id', '');
            $customerServiceGroupId = $this->request->post('customer_service_group_id', '');
            $partner_id = $this->admin['partner_id'];
            $where['partner_id'] = $partner_id;
            $adminCustomerServiceInfo = $this->getCustomerServiceInfo();
            switch ($this->admin['rule']) {
                case 1:
//                    $customerServiceGroupId = $adminCustomerServiceInfo['customer_service_personnel_info']['customer_service_group_id'];
//                    break;TODO 组长可查看组员信息功能关闭
                case 2:
                    $customerServicePersonnelId = $adminCustomerServiceInfo['customer_service_personnel_info']['id'];
                    break;
            }
            if ($customerServicePersonnelId != '') {
                $where['customer_service_personnel_id'] = $customerServicePersonnelId;
            }
            if ($customerServiceGroupId != '') {
                $where['customer_service_group_id'] = $customerServiceGroupId;
            }
            if ($status != '') {
                $where['status'] = $status;
            }
            if ($user_id != '') {
                $where['user_id'] = $user_id;
            }

            if ($cash_out_num != '') {
                $where['cash_out_num'] = ['like', '%' . $cash_out_num . '%'];
            }
            $where['is_del'] = 0;
            $list = $this->model
                ->field('*')
                ->where($where)
                ->page($page, $size)
                ->order($sort, $order)
                ->select()
                ->toArray();
            $user = new User();
            $list = $user->formatOrderData($list);
            $total = $this->model->where($where)->count('id');
            $totalPage = ceil($total / $size);
            $data = [
                'total' => $total,
                'list' => $list,
                'totalPage' => $totalPage
            ];
            $this->success('列表', '', $data);
        }
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if($this->admin['rule'] == 3){
            $this->error('非客服不可操作!');
        }
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error('不存在的数据', '', [], 404);
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = $name . '.edit';
                        $row->validateFailException(true)->validate($validate);
                    }
                    $params['admin_id'] = $this->admin['id'];
                    $result = $row->allowField(true)->isUpdate(true)->save($params);
                    if (!empty($params['status'])) {
                        if ($params['status'] == 3) {
                            //修改用户状态
                            $result = \app\partneradmin\model\User::update(['current_status' => 34], ['id' => $row['user_id']]);
                        } elseif ($params['status'] == 2) {
                            //修改用户状态
                            $result = \app\partneradmin\model\User::update(['current_status' => 35], ['id' => $row['user_id']]);
                        }elseif ($params['status'] == 1) {
                            //修改用户状态
                            $result = User::update(['current_status' => 33], ['id' => $row['user_id']]);
                        }
                    }

                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success('修改成功');
                } else {
                    $this->error('修改失败');
                }
            }
            $this->error('参数不能为空！');
        }
    }

    /**
     * 数据的设置操作, 目前有  开关  是否默认  状态
     */
    public function setup($ids = "")
    {
        if($this->admin['rule'] == 3){
            $this->error('非客服不可操作!');
        }
        $type = $this->request->post("type");
        $val = $this->request->post("val");
        $field = $type;
        switch ($type) {
            case 'switch':
            case 'status':
                $data[$field] = $val;
                $where['id'] = ['in', $ids];
                break;
            case 'is_default':
                if ($val == 1) {
                    $oldwhere['id'] = ['neq', $ids];
                    $oldwhere['is_default'] = 1;
                    $this->model->update(['is_default' => 0], $oldwhere);
                    $data[$field] = $val;
                    $where['id'] = $ids;
                }
                break;
            default:
                $this->error('不存在的业务');
                break;
        }
        if ($type == 'status') {
            $row = $this->model->get($ids);
            if ($row['status'] == 2) {
                $this->error('提现订单修改为失败后,不允许修改为其他状态!');
            }
            if ($val == 3) {
                //修改用户状态
                $result = User::update(['current_status' => 34], ['id' => $row['user_id']]);
            } elseif ($val == 2) {
                //修改用户状态
                $result = User::update(['current_status' => 35], ['id' => $row['user_id']]);
            }elseif ($val == 1) {
                //修改用户状态
                $result = User::update(['current_status' => 33], ['id' => $row['user_id']]);
            }
        }
        $this->model->update($data, $where);
        $this->success('操作成功');
    }
}